package com.example.crm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.crm.entity.Contact;
import com.example.crm.service.ContactService;
import com.example.crm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 联系人API控制器
 */
@RestController
@RequestMapping("/api/contact")
public class ContactAPIController {

    @Autowired
    private ContactService contactService;
    
    /**
     * 分页查询联系人列表
     */
    @GetMapping("/list")
    public Result<IPage<Contact>> list(@RequestParam(required = false) Map<String, Object> params) {
        if (params == null) {
            params = new HashMap<>();
        }
        IPage<Contact> page = contactService.listContactsByPage(params);
        return Result.success(page);
    }
    
    /**
     * 根据客户ID获取联系人列表
     */
    @GetMapping("/customer/{customerId}")
    public Result<List<Contact>> getByCustomerId(@PathVariable Long customerId) {
        List<Contact> contacts = contactService.getContactsByCustomerId(customerId);
        return Result.success(contacts);
    }
    
    /**
     * 获取联系人详情
     */
    @GetMapping("/{id}")
    public Result<Contact> getById(@PathVariable Long id) {
        Contact contact = contactService.getContactDetail(id);
        return Result.success(contact);
    }
    
    /**
     * 新增联系人
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody Contact contact) {
        contact.setCreateTime(LocalDateTime.now());
        contact.setUpdateTime(LocalDateTime.now());
        contact.setDeleted(0);
        
        // 如果是首要联系人，则设置其他联系人为非首要
        if (contact.getIsPrimary() != null && contact.getIsPrimary()) {
            Contact primaryContact = contactService.getPrimaryContactByCustomerId(contact.getCustomerId());
            if (primaryContact != null) {
                primaryContact.setIsPrimary(false);
                contactService.updateById(primaryContact);
            }
        }
        
        boolean result = contactService.save(contact);
        return Result.success(result);
    }
    
    /**
     * 更新联系人
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody Contact contact) {
        contact.setUpdateTime(LocalDateTime.now());
        boolean result = contactService.updateContact(contact);
        return Result.success(result);
    }
    
    /**
     * 删除联系人
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean result = contactService.removeById(id);
        return Result.success(result);
    }
    
    /**
     * 设置首要联系人
     */
    @PostMapping("/primary/{id}")
    public Result<Boolean> setPrimary(@PathVariable Long id) {
        Contact contact = contactService.getById(id);
        if (contact == null) {
            return Result.error("联系人不存在");
        }
        
        boolean result = contactService.setPrimaryContact(id, contact.getCustomerId());
        return Result.success(result);
    }
} 